package com.skyworthclub.competition.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.skyworthclub.competition.beans.Comment;
import com.skyworthclub.competition.service.CommentService;
import com.skyworthclub.competition.utils.web.DataValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  评论管理前端控制器
 * </p>
 *
 * @author li
 * @since 2018-10-15
 */
@Controller
@RequestMapping("/api/comment")
public class CommentController {

    private final CommentService commentService;

    @Autowired
    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    /**
     * @param matchId   比赛id
     * @param pageNum   页码
     * @return  评论列表
     */
    @GetMapping("/list/{matchId}/{pageNum}")
    public ResponseEntity<Map<String, Object>> listAllComments(@PathVariable Integer matchId,
                                                               @PathVariable Integer pageNum) {
        Page<Comment> commentPage = commentService.getPage(matchId ,pageNum);
        Map<String, Object> map = new HashMap<>(4);
        map.put("status", 200);
        map.put("message", "请求成功！");
        map.put("commentPage", commentPage);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    /**
     * 获取顶层评论
     */
    @GetMapping("/top/{matchId}/{pageNum}")
    public ResponseEntity<Map<String, Object>> getTopComments(@PathVariable Integer matchId,
                                                              @PathVariable Integer pageNum) {

        Page<Comment> topComments = commentService.getTopComments(matchId, pageNum);
        Map<String, Object> map = new HashMap<>(4);
        map.put("status", 200);
        map.put("message", "请求成功！");
        map.put("commentPage", topComments);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }


    /**
     * 获取子评论列表
     */
    @GetMapping("/child/{parentId}/{pageNum}")
    public ResponseEntity<Map<String, Object>> getChildComments(@PathVariable Integer parentId,
                                                                @PathVariable Integer pageNum) {
        Page<Comment> childComments = commentService.getChildComments(parentId, pageNum);
        Map<String, Object> map = new HashMap<>(4);
        map.put("status", 200);
        map.put("message", "请求成功！");
        map.put("commentPage", childComments);
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    /**
     * 添加评论
     *
     * @param matchId 比赛id
     * @param comment 评论对象
     * @return 评论对象的id
     */
    @PostMapping("/post/{matchId}")
    public ResponseEntity<Map<String, Object>> addComment(@PathVariable Integer matchId,
                                                          @RequestBody @Valid Comment comment,
                                                          Errors errors) {
        Map<String, Object> map = new HashMap<>(4);
        //数据校验
        ResponseEntity<Map<String, Object>> responseEntity = DataValidateUtil.validate(errors);
        if (responseEntity != null) {

            return responseEntity;

        } else if (comment.getParentId() != null || comment.getReplyTo() != null) {
            map.put("status", 400);
            map.put("message", "数据不合法");
            map.put("errors", "含有parentId或replyTo多余字段");
            return new ResponseEntity<>(map, HttpStatus.BAD_REQUEST);

        }
        comment.setMatchId(matchId);
        boolean insert = commentService.insertComment(comment);
        if (insert) {
            map.put("status", 200);
            map.put("message", "评论成功！");
            map.put("comment", comment);
        } else {
            map.put("status", 400);
            map.put("message", "评论失败！");
        }

        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    /** 添加回复
     * @param matchId   比赛id
     * @param replyComment  回复评论对象
     * @return  回复评论对象id
     */
    @PostMapping("/reply/{matchId}")
    public ResponseEntity<Map<String, Object>> addReply(@PathVariable Integer matchId,
                                                        @RequestBody @Valid Comment replyComment,
                                                        Errors errors) {
        Map<String, Object> map = new HashMap<>(4);
        //数据校验
        ResponseEntity<Map<String, Object>> responseEntity = DataValidateUtil.validate(errors);
        if (responseEntity != null) {

            return responseEntity;

        } else if (replyComment.getParentId() == null || replyComment.getReplyTo() == null) {
            map.put("status", 400);
            map.put("message", "数据不合法");
            map.put("errors", "缺失parentId或replyTo字段");
            return new ResponseEntity<>(map, HttpStatus.BAD_REQUEST);

        }

        replyComment.setMatchId(matchId);
        boolean insertComment = commentService.insertComment(replyComment);
        if (insertComment) {
            map.put("status", 200);
            map.put("message", "回复评论成功！");
            map.put("replyComment", replyComment);
        } else {
            map.put("status", 400);
            map.put("message", "回复评论失败！");
        }
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    /** 删除评论
     * @param commentId 评论的id
     * @return  成功或失败
     */
    @DeleteMapping("/delete/{commentId}")
    public ResponseEntity<Map<String, Object>> deleteComment(@PathVariable Integer commentId) {
        Map<String, Object> map = new HashMap<>(4);
        boolean delete = commentService.deleteCommentById(commentId);
        if (delete) {
            map.put("status", 200);
            map.put("message", "删除成功");
        } else {
            map.put("status", 400);
            map.put("message", "删除失败");
        }
        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    /** 修改评论内容
     * @param commentId 评论对象id
     * @param comment   评论对象
     * @return  成功或失败
     */
    @PostMapping("/edit/{commentId}")
    public ResponseEntity<Map<String, Object>> editComment(@PathVariable Integer commentId,
                                                           @RequestBody Comment comment) {
        comment.setCommentId(commentId);
        Map<String, Object> map = new HashMap<>(4);

        boolean update = commentService.updateCommentById(comment);
        if (update) {
            map.put("status", 200);
            map.put("message", "修改成功");
        } else {
            map.put("status", 200);
            map.put("message", "修改失败");
        }
        return new ResponseEntity<>(map, HttpStatus.OK);
    }
}

